package com.sicheng.lc.周赛.分类.秒杀子数组类题目基本原理_前缀和_滑动窗体_数论;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/7/11 13:51
 */
public class 长度最小的子数组 {
    //    https://leetcode.cn/problems/minimum-size-subarray-sum/
    public int minSubArrayLen(int target, int[] nums) {
        int l = 0;
        long sum = 0;
        int res = Integer.MAX_VALUE;
        for (int r = 0; r < nums.length; r++) {
            sum += nums[r];
            while (l <= r && sum >= target) {
                res = Math.min(res, r - l + 1);
                sum -= nums[l];
                l++;
            }
        }
        return res == Integer.MAX_VALUE ? 0 : res;
    }
}
